<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/quocha/lib/Sanpham.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/quocha/lib/DanhMuc.php';
$sanphamOb = new Sanpham();
$danhmucOb = new DanhMuc();
$danhmucOb->danhmucCombox();
$sanpham = array(
    'id' => null,
    'ten' => null,
    'danhmuc_id' => null,
    'gia' => null,
    'mota' => null,
    'dacbiet' => 0,
    'size' => null,
    'chatlieu' => null,
    'mau' => null,
    'hinhanh' => null,
    'trangthai' => 1
);
if (isset($_GET['id'])) {
    $sanpham = $sanphamOb->laySanPham($_GET['id']);
}

if (isset($sanpham['id'])) {
    $command = 'edit';
} else {
    $command = 'new';
}
$message = array();
if (isset($_POST['capnhat'])) {
    if ($_POST['command'] == 'new') {
        if ($sanphamOb->themmoiSanPham($_POST)) {
            $sanphamOb->uploadFile($_FILES['hinhanh'], DataProvider::$LAST_INSERT_ID);
            header('Location: /quocha/admin/sanpham.php');
        } else {
            $message[] = "Cập nhật thất bại";
        }
    } else {
        if ($sanphamOb->capnhapSanPham($_GET['id'], $_POST)) {
            if (isset($_POST['xoa_anh']) && $_POST['xoa_anh'] == 'yes') {
                $isDel = true;
            } else {
                $isDel = false;
            }
            $sanphamOb->uploadFile($_FILES['hinhanh'], $_GET['id'], 'edit', $isDel);
            header('Location: /quocha/admin/sanpham.php');
        } else {
            $message[] = "Cập nhật thất bại";
        }
    }
}
?>

<div class="grid_12">
    <div class="box round first fullpage">
        <h2>Thông tin sản phẩm</h2>
        <div class="block ">
            <form method="post" action="" enctype="multipart/form-data">
                <input type="hidden" value="<?php echo $command ?>" name="command"/>
                <table class="form">
                    <?php if (count($message) > 0): ?>
                        <tr>
                            <td colspan="2">
                                <?php foreach ($message as $mes): ?>
                                    <p><?php echo $mes ?></p>
                                <?php endforeach; ?>
                            </td>         
                        </tr>
                    <?php endif; ?>
                    <tr>
                        <td >Tên sản phẩm:</td>
                        <td><input type="text" value="<?php echo $sanpham['ten'] ?>"  name="ten" id="ten" size="40px" /></td>
                    </tr>
                    <tr>
                        <td >Thuộc danh mục:</td>
                        <td>
                            <select name="danhmuc_id">
                                <option value="">[ Chọn ]</option>
                                <?php foreach ($danhmucOb->danhmucCombox() as $value => $title): ?>
                                    <option <?php echo ($value == $sanpham['danhmuc_id']) ? "selected" : ""; ?> value="<?php echo $value ?>"><?php echo $title ?></option>
                                <?php endforeach; ?>
                            </select> 
                        </td>
                    </tr>
                    <tr>
                        <td >Giá:</td>
                        <td>
                            <input type="text" value="<?php echo $sanpham['gia'] ?>"  name="gia" id="gia" size="40px" />
                        </td>
                    </tr>
                    <tr>
                        <td>Mô tả:</td>
                    </tr>
                    <tr>
                        <td ></td>
                        <td>
                            <textarea class="ckeditor" name="mota" id="mota" style="width: 80%;height: 500px"><?php echo htmlspecialchars($sanpham['mota'], ENT_QUOTES, 'UTF-8') ?>
                            </textarea>
                        </td>
                    </tr>
                    <tr>
                        <td >Đặc biệt:</td>
                        <td>
                            <select name="dacbiet" id="dacbiet">
                                <?php foreach (Sanpham::$DUNG_SAI_COMBO as $value => $title): ?>
                                    <option <?php echo ($value == $sanpham['dacbiet']) ? "selected" : ""; ?>value="<?php echo $value ?>"><?php echo $title ?></option>
                                <?php endforeach; ?>    
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td >Size:</td>
                        <td>
                            <input type="text" value="<?php echo $sanpham['size'] ?>" name="size" id="size" size="40px" />
                        </td>
                    </tr>
                    <tr>
                        <td >Chất liệu:</td>
                        <td>
                            <input type="text" value="<?php echo $sanpham['chatlieu'] ?>"  name="chatlieu" id="chatlieu" size="40px" />
                        </td>
                    </tr>
                    <tr>
                        <td >Màu:</td>
                        <td>
                            <input type="text" value="<?php echo $sanpham['mau'] ?>"  name="mau" id="mau" size="40px" />
                        </td>
                    </tr>
                    <tr>
                        <td >Hình ảnh:</td>
                        <td>
                            <?php if ($command == 'edit' && !empty($sanpham['hinhanh']) && file_exists($_SERVER['DOCUMENT_ROOT'] . $sanpham['hinhanh'])): ?>
                                <img width="150px" src="<?php echo $sanpham['hinhanh'] ?>" />
                                <input type="checkbox" value="yes" name="xoa_anh"/> Xóa ảnh
                            <?php endif; ?>
                            <input type="file" value="<?php echo $sanpham['hinhanh'] ?>"  name="hinhanh" id="hinhanh" size="40px" />
                        </td>
                    </tr>
                    <tr>
                        <td >Trạng thái:</td>
                        <td>
                            <select name="trangthai" id="trangthai">
                                <?php foreach (Sanpham::$AN_HIEN_COMBO as $value => $title): ?>
                                    <option <?php echo ($value == $sanpham['trangthai']) ? "selected" : ""; ?> value="<?php echo $value ?>"><?php echo $title ?></option>
                                <?php endforeach; ?>  
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><input class="btn" type="submit" name="capnhat" id="capnhat" value="Cập nhật"</td>
                    </tr>
                </table>
            </form>

